package cn.scau.server.service.impl;

import cn.scau.server.mapper.EmpMapper;
import cn.scau.server.pojo.Dept;
import cn.scau.server.pojo.Emp;
import cn.scau.server.pojo.EmpPos;
import cn.scau.server.mapper.EmpPosMapper;
import cn.scau.server.pojo.Position;
import cn.scau.server.service.DeptService;
import cn.scau.server.service.EmpPosService;
import cn.scau.server.service.PositionService;
import cn.scau.server.vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 代码不跑我先跑
 * @since 2025-03-30
 */
@Service
public class EmpPosServiceImpl extends ServiceImpl<EmpPosMapper, EmpPos> implements EmpPosService {
    @Autowired
    private PositionService positionService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private EmpMapper empMapper;

    // 修改员工职位信息
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO updateEmpPosition(String empId, String deptName, String positionName, Integer isLeader) {
        try {
            // 查询部门 ID
            QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
            deptQueryWrapper.eq("name", deptName);
            Dept dept = deptService.getOne(deptQueryWrapper);
            if (dept == null) {
                return ResultVO.fail("部门不存在");
            }
            Integer deptId = dept.getId();

            // 查询职位 ID
            QueryWrapper<Position> positionQueryWrapper = new QueryWrapper<>();
            positionQueryWrapper.eq("name", positionName);
            Position position = positionService.getOne(positionQueryWrapper);
            if (position == null) {
                return ResultVO.fail("职位不存在");
            }
            Integer positionId = position.getId();

            // 如果 isLeader 为 1，则需要检查该部门是否已经存在一个领导
            if(isLeader == 1) {
                QueryWrapper<EmpPos> checkLeaderQueryWrapper = new QueryWrapper<>();
                checkLeaderQueryWrapper.eq("dept_id", deptId);
                checkLeaderQueryWrapper.eq("is_leader", 1);

                // 查询 EmpPos 表中的领导员工编号
                List<EmpPos> existingLeaders = list(checkLeaderQueryWrapper);

                // 遍历查询结果，检查对应的员工是否未被逻辑删除
                for (EmpPos leader : existingLeaders) {
                    QueryWrapper<Emp> empQueryWrapper = new QueryWrapper<>();
                    empQueryWrapper.eq("emp_id", leader.getEmpId());
                    empQueryWrapper.eq("is_deleted", 0); // 确保员工未被逻辑删除
                    Emp emp = empMapper.selectOne(empQueryWrapper);

                    if (emp != null && !emp.getEmpId().equals(empId)) {
                        return ResultVO.fail("该部门已经存在一个领导，无法再设置其他员工为领导");
                    }
                }
            }
            // 更新 EmpPos 数据
            QueryWrapper<EmpPos> empPosQueryWrapper = new QueryWrapper<>();
            empPosQueryWrapper.eq("emp_id", empId);
            EmpPos empPos = getOne(empPosQueryWrapper);
            if (empPos == null) {
                return ResultVO.fail("员工职位信息不存在");
            }
            empPos.setDeptId(deptId);
            empPos.setPositionId(positionId);
            empPos.setIsLeader(isLeader);

            boolean updateResult = updateById(empPos);
            if (updateResult) {
                return ResultVO.success("员工职位信息更新成功");
            } else {
                return ResultVO.success("员工职位信息更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.fail("系统异常，请稍后再试");
        }
    }

}
